﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrbaCommerce.Historial_Cliente
{

    public partial class ListadoOfertas: Form
    {
        private string strFiltros;
        private int user_id;

        // Page
        private int mintTotalRecords = 0;
        private int mintPageSize = 10;
        private int mintPageCount = 0;
        private int mintCurrentPage = 1;


        private void fillGrid()
        {
            // For Page view.
            //this.mintPageSize = int.Parse(this.bindingNavigator1.Text);
            this.mintTotalRecords = getCount();
            this.mintPageCount = this.mintTotalRecords / this.mintPageSize;

            // Adjust page count if the last page contains partial page.
            if (this.mintTotalRecords % this.mintPageSize > 0)
                this.mintPageCount++;

            this.mintCurrentPage = 0;

            loadPage();
        }

        private int getCount()
        {
            // This select statement is very fast compare to SELECT COUNT(*)

            SqlConnection con = Program.obtenerConexion();
            string strSql = "SELECT ofe_sub_id as 'Código'"
                + " FROM Monkey_Coders.Ofertas_subasta, Monkey_Coders.Subastas"
                + strFiltros;
                
                /*"SELECT Rows FROM SYSINDEXES " +
                            "WHERE Id = OBJECT_ID('Monkey_Coders.Publicaciones') AND IndId < 2";*/

            SqlCommand cmd = new SqlCommand(strSql, con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "Monkey_Coders.Subastas");

            int intCount = ds.Tables[0].Rows.Count;


            cmd.Dispose();

            return intCount;
        }

        private void loadPage()
        {
            string strSql = "";
            int intSkip = 0;

            if (mintPageCount == 0)
                return;

            SqlConnection con = Program.obtenerConexion();
            intSkip = (this.mintCurrentPage * this.mintPageSize);

            // Select only the n records.

            strSql = "SELECT TOP " + this.mintPageSize
                + " ofe_sub_id as 'Código', sub_publicacion_id as 'Publicación', ofe_sub_monto as 'Monto',"
                + " (CASE WHEN ofe_sub_id = sub_oferta_ganadora_id THEN 'Si'"
                + " ELSE 'No' END) as 'Ganadora'"
                + " FROM Monkey_Coders.Ofertas_subasta, Monkey_Coders.Subastas"
                + strFiltros
                + " AND ofe_sub_id NOT IN " +
                "(SELECT TOP " + intSkip +
                " ofe_sub_id "
                + " FROM Monkey_Coders.Ofertas_subasta, Monkey_Coders.Subastas"
                + strFiltros + ")";


            SqlCommand cmd = new SqlCommand(strSql, con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            da.Fill(ds, "Monkey_Coders.Subastas");

            // Populate Data Grid
            dataGridView1.DataSource = ds.Tables["Monkey_Coders.Subastas"].DefaultView;

            // Show Status
            labelPaginas.Text = (this.mintCurrentPage + 1).ToString() +
              " / " + this.mintPageCount.ToString();

            cmd.Dispose();
            da.Dispose();
            ds.Dispose();
        }





        public ListadoOfertas(int user_id)
        {
            this.user_id = user_id;

            InitializeComponent();

        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();

            strFiltros = " WHERE ofe_sub_subasta_id = sub_id  AND ofe_sub_usuario_id = " + user_id;

            if (checkBoxGanadoras.Checked == true)
                strFiltros = strFiltros + " AND ofe_sub_id = sub_oferta_ganadora_id"; 

            fillGrid();

        }

        private void buttonLimpiar_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();
        }

        private void buttonPrimera_Click(object sender, EventArgs e)
        {

            this.mintCurrentPage = 0;

            loadPage();
        }

        private void buttonAtras_Click(object sender, EventArgs e)
        {
            if (this.mintCurrentPage == this.mintPageCount)
                this.mintCurrentPage = this.mintPageCount - 1;

            this.mintCurrentPage--;

            if (this.mintCurrentPage < 1)
                this.mintCurrentPage = 0;

            loadPage();
        }

        private void buttonAdelante_Click(object sender, EventArgs e)
        {
            this.mintCurrentPage++;

            if (this.mintCurrentPage > (this.mintPageCount - 1))
                this.mintCurrentPage = this.mintPageCount - 1;

            loadPage();
        }

        private void buttonLast_Click(object sender, EventArgs e)
        {
            this.mintCurrentPage = this.mintPageCount - 1;
            loadPage();
        }

        private void ListadoCompras_Load(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

    }
}
